define(function(require,exports,module) {
    var myPub = require('./public');

    var goodsDateUrl = '/myInfo/findByMenberIdPage';    //模拟数据链接
    var oGoodsListBox = $('.js_goodsList');


    var oWindow = $(window);
    var oWrap = $('.js_wrap');

    var oLoadingBtn = $('.js_loadingBtn');
    var oLoading = oLoadingBtn.eq(1) ;       // 加载按钮
    var oLoadingNull = oLoadingBtn.eq(0) ;   // 没有更多选项

    var oErrPage = $('.js_errPage');         // 无数据的提示页面

    module.exports = {
        init: function() {
            connectHtml.AjaxData(connectHtml.start, connectHtml.limit);
            connectHtml.ScrollPage();
        }
    };



    var connectHtml = {
        limit: 20,                       // 每次加载多少数据
        getData: [],                    // 数据记录
        pagearr: 1,                     // 当前页码
        pageCout: 0,                    // 页码总数
        start:0,                        // 开始数据
        end: 0,                         // 结束数据
        time_Scroll:null,               // 计时器
        bLoadOff: true,                 // 加载开关
        // 设置广告数据
        AjaxData: function (start, limit , fn) {
            var _this = this;
            G_JSON_POST(goodsDateUrl,{
                'start': _this.start,
                'end': _this.limit
            },function (data) {
                if( data.status == "error") {
                    oWrap.hide();
                    oErrPage.show();
                    return false;
                }
                // 存储数据
                _this.getData = data.obj;

                // 获取总页数
                // _this.pageCout = Math.ceil(_this.getData.length / _this.limit);

                if( data.status == "success") {

                    // 没有数据和页数1页时候 显示错误页面
                    if(_this.getData.length == 0 && _this.pagearr < 2) {
                        _this.bLoadOff = false;
                        oWrap.hide();
                        oErrPage.show();
                        return
                    }
                    // 页数大于
                    if( _this.pagearr >=1) {
                        if( _this.getData.length == 0) {
                            oLoading.hide();
                            oLoadingNull.show();
                            _this.bLoadOff = false;
                            return
                        }

                    }

                    // 拼接页面
                    _this.GoodsLiBoxHtml(_this.getData);
                    oLoading.show();

                    if(_this.getData.length <  _this.limit) {
                        oLoading.hide();
                        oLoadingNull.show();
                        _this.bLoadOff = false;
                    }
                    data = null;
                    if(fn)fn();
                }
            }, function () {
                // start
            }, function () {
                // end
            });
        },

        ScrollPage: function () {
            var _this = this;
            var minScrHeight = 200;

            oWindow.on('scroll', function () {
                var afterScroll = $(this).scrollTop();

                // 滚动页面距离底部200像素 延迟150毫秒加载数据
                clearTimeout(_this.time_Scroll);
                _this.time_Scroll = setTimeout(function() {
                    if(oWrap[0].scrollHeight - afterScroll - oWindow.height() < minScrHeight) {
                        // 设置翻页到最后一页 返回
                        if(!_this.bLoadOff) {
                            _this.dataJson = null;
                            return;
                        }

                       // console.log( _this.pagearr)
                        // 滚动底部页数增加 起始数据等于上次结束数据  新的结束等于页数*页码数
                        _this.start = _this.limit*_this.pagearr;
                        _this.pagearr++;
                        _this.AjaxData(_this.start, _this.limit);
                    }
                },250);
            })

        },

        // 正常的数据
        GoodsLiBoxHtml : function (data, fn) {
            var _this = this;
            var shtml = '';
            var times = [];
            for (var i=0; i<data.length; i++) {
                times[i] = myPub.FormatDateTime(new Date(data[i].createDate),'minSec');
                var op = data[i].detail;
                var ops = checkOp(data[i].operation);
                var growValue = data[i].growValue;
                shtml +='<div class="growthValDetail flexBox spBe">'+
                    '    <div class="growthSource">'+
                    '        <p class="growthSourceType font3">'+ops+'</p>'+
                    //'        <p class="growthSourceNum">'+growValue+'</p>'+
                    '     </div>'+
                    '     <div class="growthNum">'+
                    '         <p class="growthValAdd">'+
                    '                <span class="addOrCut">';
                        if(op>=0){
                            shtml +="+";
                        }else{
                            shtml +="";
                        }
                shtml +=  '</span><span class="growthNumber">'+op+'</span>'+
                    '         </p>'+
                    '         <p class="growthDate textRight">'+times[i]+'</p>'+
                    '      </div>'+
                    ' </div>'
            }
            oGoodsListBox.append(shtml);

            // 高度检测
            if(oGoodsListBox.height() < oWindow.height()) {
               //console.log('不够高度');
                _this.start = _this.limit*_this.pagearr;
                _this.pagearr++;
                _this.AjaxData(_this.start, _this.limit);
            }

            if(fn)fn();

        },
    }

    function checkOp(op) {
        if(op == "PARTAFTERSALE" || op == "SHOPING" || op == "SHOPUSE"||op == "FIRSTSHOPING"||op == "SHOPDEDUCT"){
            return "购物";
        }else if(op == "LOGIN"){
            return　"每日签到";
        }else if(op == "SYSTEMSENDING"){
            return "系统发放";
        }else if(op == "TIAOZENG"){
            return "手动调增";
        }else if(op == "TIAOJIAN"){
            return "手动调减";
        }else if(op == "NOPAY"){
            return "未支付退还";
        }else if(op == "COMMENT"){
            return "评论";
        }else if(op == "COMPLETEINFORMATION" || op == "BINGMOBILE"){
            return "完善资料";
        }else if(op == "AFTERSALE" ||op == "PARTAFTERSALE" || op == "NOPAY"){
            return "退款";
        }else if(op=="MEMBERVALID"){
            return "会员到期";
        }
        else{
            return "其他";
        }
    }
})